﻿--[[
   @brief    话题直播房间窗口
   @author   rickonzhang
   @version  1.0
   @date     2016-08-08
--]]

live_room_view = 
{
}

function live_room_view.log(log_str, log_level)
    local is_true = false;
    if log_level == true then
        is_true = true;
    end
    base_utility.log("[live_room] - view - "..log_str, is_true);
end

function live_room_view.init()
    live_room_view.log("init ");
end

function live_room_view.show(group_id)
    local wnd_id = live_room_model.get_dlg_id_from_group_id(group_id);
    if wnd_id == nil then
        wnd_id = live_room_view.create_wnd(group_id);
    end
    
    if wnd_id == 0 then
        live_room_view.log("create wnd error, id:"..tostring(group_id), true);
        return;
    end

    --shadow_helper.create_shadow(wnd_id);

    local wnd_ptr = ui_utility.get_ctrl_ptr_by_id(wnd_id);
    if wnd_ptr ~= nil then
        wnd_ptr:Show(TRUE);
        local hwnd = wnd_ptr:GetHwnd();

        local tpfui =
                base_utility.get_component_adapter("ITpf_ui",
                    "ierd_tgp.tpf_ui.GetTPFUI();");
        if tpfui:IsIconic(hwnd) then
            wnd_ptr:Restore();
        end
        wnd_ptr:BringToTop();
    end
end

function live_room_view.create_wnd(group_id)
    local wnd_id = layout_mgr.restore_layout_multi("social\\social_talk_for_live_room.tpf");
    if wnd_id > 0 then
        local group_info = group_list_model.get_group_info(group_id);
        ui_utility.setproperty_by_id(wnd_id, 'Caption', group_info.name);

        live_room_model.insert_group_id_and_dlg_id_pair(group_id, wnd_id);
--        local shadow_id = layout_mgr.restore_layout_multi("wegame_social\\ChatDlgShadow.tpf");
--        ui_utility.setproperty_by_id(shadow_id, "Width", "794");
--        ui_utility.setproperty_by_id(shadow_id, "Height", "639");
--        live_room_model.insert_group_id_and_shadow_id_pair(wnd_id, shadow_id);

        local web_ctrl = ui_utility.get_ctrl_ptr_by_path(wnd_id, "QBWebBrowser_Talk");
        local dst_url = web_utility.make_login_status_url(LoadStr("SOCIA_LIVETOPIC_URL"), login_model.main_uin);
        local ui_str, str_holder = U8toCUIStr(dst_url);
        web_ctrl:Navigate(ui_str);
    end
    return wnd_id;
end

function live_room_view.close_wnd(dlg_id, real_close)
    live_room_view.log("close_wnd, dlg_id:"..tostring(dlg_id));
    if dlg_id > 0 then
        ui_utility.setproperty_by_id(dlg_id, "Visible", "false");
        if real_close == true then
            layout_mgr.delay_remove_ctrl(dlg_id);
            --shadow_helper.destroy_shadow(dlg_id);
            live_room_model.remove_dlg_id(dlg_id);
--            -- 移除阴影对话框
--            local shadow_id = live_room_model.get_shadow_id_from_dlg_id(dlg_id);
--            layout_mgr.delay_remove_ctrl(shadow_id);
--            live_room_model.remove_shadow_id(shadow_id);
        end
    end
end

function live_room_view.on_dialog_close(uilib, ctrl_id)
    ui_utility.setproperty_by_id(ctrl_id, "Visible", "false");
    return true;
end

function live_room_view.on_dialog_hide(uilib, ctrl_id)
    live_room_view.log("on_dialog_hide");
    --local shadow_id = live_room_model.get_shadow_id_from_dlg_id(ctrl_id);
    --ui_utility.setproperty_by_id(shadow_id, "Visible", "false");
    --shadow_helper.destroy_shadow(ctrl_id);
end

function live_room_view.click_join_live_room_btn(uilib, ctrl_id)
    comm_center.call_service('i_service_increase_click_live_room_entry_cnt', {});
    live_room_control.click_join_cur_live_room();
end

function live_room_view.is_room_entry_visible()
    local is_visible = false;
    if live_room_view.ui_obj_ids_ ~= nil then
        for index, ctrl_id in pairs(live_room_view.ui_obj_ids_) do
            local ctrl_ptr = ui_utility.get_ctrl_ptr_by_id(ctrl_id);
            if ctrl_ptr ~= nil then
                if ctrl_ptr:IsVisible() == TRUE then
                    is_visible = true;
                    break;
                end
            end
        end
    end
    return is_visible;
end

-- 更新话题房间UI
function live_room_view.update_live_room_info_ui()
    local room_info = live_room_model.get_cur_live_room_info();
    if room_info ~= nil and room_info.room_base_info ~= nil then
        if live_room_view.ui_obj_ids_ ~= nil then
            for index, ctrl_id in pairs(live_room_view.ui_obj_ids_) do
                local ctrl_ptr = ui_utility.get_ctrl_ptr_by_id(ctrl_id);
                if ctrl_ptr ~= nil then
                    if live_room_model.is_live_room_closed_ == true then
                        --直播入口关闭
                        ctrl_ptr:Show(FALSE);
                    else
                        local id = ui_utility.get_ctrl_id_by_path(ctrl_id, "Ctn_LiveRoomEntry\\Ctn_Header");
                        ui_utility.setproperty_by_id(id, "Background", string.format("ONEIMAGE:social_network\\common_icon\\live_room_entry_default.png|%s;Stretch;null;", room_info.room_base_info.logo));
                        ui_utility.setproperty_by_id(id, "BackgroundHover", string.format("ONEIMAGE:social_network\\common_icon\\live_room_entry_default.png|%s;Stretch;null;", room_info.room_base_info.logo));
                        ui_utility.setproperty_by_id(id, "BackgroundPressed", string.format("ONEIMAGE:social_network\\common_icon\\live_room_entry_default.png|%s;Stretch;null;", room_info.room_base_info.logo));
                        id = ui_utility.get_ctrl_id_by_path(ctrl_id, "Ctn_LiveRoomEntry\\St_Title");
                        ui_utility.setproperty_by_id(id, "Caption", room_info.room_base_info.room_title)
                        id = ui_utility.get_ctrl_id_by_path(ctrl_id, "Ctn_LiveRoomEntry\\St_Desc");
                        ui_utility.setproperty_by_id(id, "Caption", "    "..tostring(room_info.room_base_info.sub_title))

                        local str = social_utility.number_format(room_info.cur_match_info.member_cnt);
                        id = ui_utility.get_ctrl_id_by_path(ctrl_id, "Ctn_LiveRoomEntry\\St_Count");
                        ui_utility.setproperty_by_id(id, "Caption", str..LoadStr("SOCIAL_TALK_DIALOG_NUM_DISCUSS"))
                        ctrl_ptr:Show(TRUE);

                        --获取new是否显示过：
                        id = ui_utility.get_ctrl_id_by_path(ctrl_id, "Ctn_LiveRoomEntry\\Ctn_NewFlag");
                        if live_room_model.is_room_id_new_flag_showed() == false then
                            ui_utility.setproperty_by_id(id, "Visible", "true");
                        else                        
                            ui_utility.setproperty_by_id(id, "Visible", "false");
                        end
                    end
                end
            end
        end
    end 
end

function live_room_view.on_show(uilib, ctrl_id)
    local web_ctrl_id = ui_utility.get_ctrl_id_by_path(ctrl_id, "QBWebBrowser_Talk");
    local web_ctrl = ui_utility.get_ctrl_ptr_by_id(web_ctrl_id);
    if web_ctrl ~= nil then
        web_ctrl:Show(TRUE);
        web_ctrl:SetFocus();
    end
    local shadow_id = live_room_model.get_shadow_id_from_dlg_id(ctrl_id);
    ui_utility.setproperty_by_id(shadow_id, "Visible", "true");
end

function live_room_view.on_wnd_pos_changed(uilib, ctrl_id)
    local pDialog = ui_utility.get_ctrl_ptr_by_id(ctrl_id);
    local shadow_id = live_room_model.get_shadow_id_from_dlg_id(ctrl_id);
    local pShadow = ui_utility.get_ctrl_ptr_by_id(shadow_id);
    if nil ~= pDialog and nil ~= pShadow then
        local     rtThis = pDialog:GetRectEx(TRUE,TRUE);
        local     rtTemp = pShadow:GetRectEx(TRUE,TRUE);
        local     nWidth = rtThis.right - rtThis.left + 32;
        local     nHeight= rtThis.bottom - rtThis.top + 32;

        rtTemp.left  = rtThis.left - 16;
        rtTemp.top   = rtThis.top  - 6;
        rtTemp.right = rtTemp.left + nWidth;
        rtTemp.bottom= rtTemp.top  + nHeight;

        pShadow:MoveEx(rtTemp,TRUE);
    end;
end

function live_room_view.on_ui_obj_created(uilib, ctrl_id)
    if live_room_view.ui_obj_ids_ == nil then
        live_room_view.ui_obj_ids_ = {};
    end
    table.insert(live_room_view.ui_obj_ids_, ctrl_id);
    live_room_view.update_live_room_info_ui();
end

function live_room_view.set_focus_dialog(dlg_id)
    base_utility.log("[live_room_view] set_focus_dialog");
    if dlg_id > 0 then
        dlg_ptr = ui_utility.get_ctrl_ptr_by_id(dlg_id);
        if dlg_ptr ~= nil then
            dlg_ptr:SetFocus();
        end
    end
end

function live_room_view.get_dialog_hwnd(dlg_id)
    if dlg_id > 0 then
        dlg_ptr = ui_utility.get_ctrl_ptr_by_id(dlg_id);
        if dlg_ptr ~= nil then
            return dlg_ptr:GetHwnd();
        end
    end
    return 0;
end

function live_room_view.on_click_close_btn(uilib, ctrl_id)
    local dlg_id = ui_utility.get_ctrl_parent_id(ctrl_id);
    local param_table = {};
    param_table["retCode"] = 0;
    param_table["data"] = {};

    local web_ctrl_id = ui_utility.get_ctrl_id_by_path(dlg_id, "QBWebBrowser_Talk");
    if web_ctrl_id > 0 then
        web_utility.lua_call_js_by_id("dialog_close",base_utility.table_to_json(param_table), web_ctrl_id);
    end
  
    live_room_view.close_wnd(dlg_id);
end

live_room_view.init();

